/**
 * SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
 * SPDX-License-Identifier: Mulan PSL v2
 */
<template>
    <div class="select_image_form">
        <sf-form-item>
            <sf-fieldlabel class="p-l0">
                <lang>服务镜像地址</lang>
            </sf-fieldlabel>
            <sf-select v-model="address"
                       :default-width="width"
                       :allow-search="true"
                       :allow-blank="false"
                       :show-add-btn="showAddImage"
                       :options="imageList"
                       display-field="address"
                       value-field="address"
                       @on-add="_addImage" />
        </sf-form-item>
    </div>
</template>

<style lang="less">
.select_image_form {
    .add-image__btn{
        margin-left: 104px;
        margin-bottom: 10px;
        margin-top:-5px;
    }
}
</style>

<script>

import deployUrlAdapter from 'src/home/mod_common/deploy_url_adapter';

import store from 'src/vuex/store';
let userID = store.state.userId;
const IMAGE_URLS = {
    get: deployUrlAdapter(`users/${userID}/simpleimages`),
    post:deployUrlAdapter(`users/${userID}/images`)
};
import addImageForm from 'src/home/mod_cluster/common/common_form/add_image_form.vue';
export default {
    props:{
        submitData:{
            type:Object,
            default:()=>{
                return {
                    image:'',
                    imageName:''
                };
            }
        },
        width:{
            type:[String, Number],
            default:336
        },
        showAddImage:{
            type:Boolean,
            default:false
        },
        imageType:{
            type:String,
            default:'public_service'
        }
    },
    data () {
        return {
            searchParams: {
                keyword:''
            },
            imageList:[],
            address:''
        };
    },


    mounted () {
        this._getImageList();
    },
    methods: {
        reset () {
            this.address = '';
        },

        // 刷新grid
        async _getImageList (firstFlag = false) {
            this.$mask();
            this.searchParams.tagID = this.imageType;
            let jsonData = await this.$ajax.get(IMAGE_URLS.get, this.searchParams);
            this.$unmask();
            let {success, msg, data} = jsonData;
            if (!success) {
                this.$showErr(msg || _('加载数据失败'));
                return;
            };
            this.imageList = data;
            this.address = firstFlag ? this.imageList[0].value : this.submitData.image;
        },
        setJsonValue (data) {
            this.address = data.image;
            Object.assign(this.submitData, data);
        },

        getJsonValue () {
            if (this.address) {
                this.submitData.imageName = this.address;
                this.submitData.image = this.address;
            }
            return this.submitData;
        },

        // 新增数据源弹窗
        _createImageFormWin () {
            if (!this.imageFormWin) {
                this.imageFormWin = this.$modal(addImageForm, {
                    autoDestory: true
                });
            }
            return this.imageFormWin;
        },

        // 新增推理服务模板
        _addImage () {
            this._createImageFormWin();
            this.imageFormWin.open({
                reset: true,
                title: _('新增服务镜像'),
                submit: () => {
                    let ajaxParams = this.imageFormWin.formRoot.getJsonValue();
                    this.ajaxSubmitImageInfo(ajaxParams, this.imageFormWin);
                }
            });
        },

        // 新增/编辑算法的卡片信息
        async ajaxSubmitImageInfo (params, formWin) {
            formWin.$mask();
            let jsonData = await this.$ajax.post(IMAGE_URLS.post, params);
            formWin.$unmask();
            let {success, msg} = jsonData;
            if (!success) {
                return this.$showErr(msg || _('保存失败'));
            }
            this.$ok(_('保存成功'));
            formWin.hide();
            this._getImageList(true);
        }
    }
};
</script>
